//
//  QRCodeCardViewController.swift
//  DSWeiBo
//
//  Created by Frank Wang on 7/9/16.
//  Copyright © 2016 Frank Wang. All rights reserved.
//

import UIKit

class QRCodeCardViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //1.设置标题
        navigationItem.title = "我的名片"
        //2添加图片容器
        view.addSubview(iconView)
        //3.布局图片容器
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: view, size: CGSizeMake(200, 100))
        //4.生成二维码
        let qrcodeImage = createQRCodeImage()
        
        //5.将生成好的二维码添加到图片容器
        iconView.image = qrcodeImage
        // Do any additional setup after loading the view.
    }

        private func createQRCodeImage() -> UIImage{
                
                //1.创建滤镜
                let filter = CIFilter(name: "CIQRCodeGenerator")
                //2.还原滤镜的默认属性
                filter?.setDefaults()
                //3.设置需要生成二维码的数据
                filter?.setValue("wangchunyang".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
                //4.从滤镜中取出生成好的图片
                let ciImage = filter?.outputImage
                let bgImage = createNonInterpolatedUIImageFormCIImage(ciImage!, size: 300)
                //5.创建一个头像
                let icon = UIImage(named: "nange")
                
                //6.合成图片（将二维码和头像进行合并）
                let newImage = creteImage(bgImage, iconImage: icon!)
                
                //7.返回生成好的二维码
                return newImage
        }
        
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK :-lazy
        
        private func creteImage(bgImage:UIImage, iconImage: UIImage) -> UIImage{
                //1.开启图片的上下文
                UIGraphicsBeginImageContext(bgImage.size)
                //2.绘制背景图片
                bgImage.drawInRect(CGRect(origin: CGPoint.zero, size: bgImage.size))
                //3.绘制头像
                let width:CGFloat = 50.0
                let height:CGFloat = width
                let x = (bgImage.size.width - width) * 0.5
                let y = (bgImage.size.height - height) * 0.5
                iconImage.drawInRect(CGRectMake(x, y, width, height))
                
                //4.取出绘制好的头像
                let newImage = UIGraphicsGetImageFromCurrentImageContext()
                
                //5.关闭上下文
                UIGraphicsEndImageContext()
                //6.返回合成的图片
                return newImage
                //7.二维码顶部的三个方块不能盖住。
        }
        
        
        private lazy var iconView:UIImageView = UIImageView()
        
        /**
         根据CIImage生成指定大小的高清UIImage
         
         :param: image 指定CIImage
         :param: size    指定大小
         :returns: 生成好的图片
         */
        private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
                
                let extent: CGRect = CGRectIntegral(image.extent)
                let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
                
                // 1.创建bitmap;
                let width = CGRectGetWidth(extent) * scale
                let height = CGRectGetHeight(extent) * scale
                let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
                let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
                
                let context = CIContext(options: nil)
                let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)
                
                CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
                CGContextScaleCTM(bitmapRef, scale, scale);
                CGContextDrawImage(bitmapRef, extent, bitmapImage);
                
                // 2.保存bitmap到图片
                let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
                
                return UIImage(CGImage: scaledImage)
        }

}
